package com.helloscala.model

import org.joda.time.DateTime
import org.squeryl.KeyedEntity

import yangbajing.persistence.SquerylEntrypoint._
import yangbajing.util.Imports.Y

case class MRole(
                  id: String,
                  comment: Option[String] = None,
                  created_at: DateTime = DateTime.now()) extends KeyedEntity[String]

object MRole {
  def findById(userId: String): List[MRole] =
    transaction(from(Entities.usersRoles, Entities.roles)((ur, r) =>
      where(
        ur.role_id === r.id and
          (ur.user_id === userId)
      ) select (r) orderBy (r.created_at.desc)).toList)


  def insert(ids: String*) = Y.tryBox(transaction {
    Entities.roles insert ids.map(v => new MRole(v))
  })
}
